const { app, powerMonitor, powerSaveBlocker } = require('electron')

const eventNames = [
  'suspend',
  'resume',
  'on-ac',
  'on-battery',
  'shutdown',
  'lock-screen',
  'unlock-screen',
  'user-did-become-active',
  'user-did-resign-active',
]

setInterval(() => {
  console.log('系统空闲状态', powerMonitor.getSystemIdleState(10))
  console.log('系统空闲时间', powerMonitor.getSystemIdleTime())
}, 3000)

app.whenReady().then(() => {
  // 事件监听
  eventNames.forEach((name) => {
    powerMonitor.on(name, (...args) => {
      console.log(`监听到事件${name}`, args)
    })
  })
  console.log('是否正在使用电池电量', powerMonitor.isOnBatteryPower())

  // 阻止系统进入 sleep 或 suspension 状态
  const id = powerSaveBlocker.start('prevent-display-sleep') // prevent-app-suspension
  console.log('开启 powerSaveBlocker', powerSaveBlocker.isStarted(id))
  setTimeout(() => {
    powerSaveBlocker.stop(id)
    console.log('检查 powerSaveBlocker 是否开启', powerSaveBlocker.isStarted(id))
  }, 5000)

  // 延缓关机
  powerMonitor.on('shutdown', (e) => {
    e.preventDefault()
  })
})
